<template>
  <div>
    <!-- 表格 -->
    <el-table :key='tableKey' :data="list" v-loading="listLoading" border fit highlight-current-row
      :style="{height: tableHeight}" style="overflow: auto;">
      <el-table-column align="center" label="序号" width="60px">
        <template slot-scope="scope">
          <span>{{ scope.$index +1 + (listQuery.pageSize*(listQuery.page-1))}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="username" label="用户名"></el-table-column>
      <el-table-column align="center" prop="realName" label="姓名"></el-table-column>
      <el-table-column align="center" prop="phone" label="手机号"></el-table-column>
      <el-table-column align="center" prop="deptName" label="部门名称"></el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button :disabled="scope.row.username=='admin'" @click="handleDelete(scope.row.id)" size="small" type="danger" v-waves icon="el-icon-delete">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div v-show="!listLoading" class="pagination-container">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page.sync="listQuery.page" :page-sizes="[10,20,30, 50]" :page-size="listQuery.pageSize"
        layout="total, sizes, prev, pager, next, jumper" :total="total"></el-pagination>
    </div>
  </div>
</template>

<script>
  import {
    getUsersByRole
  } from '@/api/sys/user/index'
  import {
    unRelateUser
  } from '@/api/sys/role/index'

  /* 工具 */
  import {
    mapGetters
  } from 'vuex'
  import * as common from '@/utils/base/common'

  export default {
    /* 用户列表 */
    name: 'user-list',
    data() {
      return {
        tableHeight: parseInt(parseInt(document.documentElement.clientHeight)) / 2 + 'px',

        restPath: 'sys/user',
        list: null,
        total: null,
        tableKey: 0,
        listLoading: true,
        listQuery: {
          page: 1,
          pageSize: 10,
          username: '',
        },
        btnDisabled: false, //按钮禁用状态

      }
    },
    props: ['roleId'],
    created() {
      this.initData()
    },
    methods: {

      initData() {
        this.getList()
      },

      // 后台获取列表数据
      getList() {
        this.listLoading = true;
        getUsersByRole(this.roleId, this.listQuery).then(response => {
          this.list = response.data.records
          this.total = response.data.total
          this.listLoading = false;
        }).catch(err => {
          this.listLoading = false;
          this.btnDisabled = true // 按钮禁用
          common.message(this, 'error', 2, '调用接口失败!')
          console.error(err)
        })
      },

      handleDelete(userId) {
        this.$confirm('确认关联用户所选用户吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          unRelateUser(this.roleId, userId).then(response => {
            if (response.code === 200) {
              common.message(this, 'success', 2, '删除成功!')
              this.getList()
            } else {
              common.tips(this, response.message)
            }
          }).catch(err => {
            common.message(this, 'error', 2, '删除失败!')
            console.error(err)
          })
        }).catch(() => {});
      },

      // 刷新页面
      flushList() {
        this.closeDialog()
        this.getList()
      },

      handleFilter() {
        this.listQuery.page = 1
        this.getList();
      },
      handleSizeChange(val) {
        this.listQuery.pageSize = val;
        this.getList();
      },
      handleCurrentChange(val) {
        this.listQuery.page = val;
        this.getList();
      },

    }
  }
</script>

<style scoped>
  @import "~@/styles/list.css";
</style>
